package com.study.exception;

import com.study.common.BaseResponse;
import com.study.common.ErrorCode;
import com.study.service.UserServiceImpl;
import com.study.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author fruity
 */
@RestControllerAdvice
//通过SpringAOP实现抛出异常
public class GlobalExceptionHandler {

    /**
     * 错误日志
     */
    private static Logger log= LoggerFactory.getLogger(UserServiceImpl.class);

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("businessException"+e.getMessage(), e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }
}
